package assets{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import model.AppModel;
	
	public class User extends Sprite{
		//properties
		private var appModel:AppModel;
		private var number:Number;
		private var userKaartenArr:Array;
		private var randomNumberArr:Array;
		private var username:String;
		private var userNameDisplay:TxtField;
		private var card:UserCard;
		private var xPos:uint;
		
		//Getters & setters
		
		//constructor
		public function User(name:String){
			trace("constructor [user]");
			
			this.appModel = AppModel.getInstance();
			userKaartenArr = [];
			randomNumberArr = [];
			
			username = name;
			trace("userName: "+username);
			kiesRandomNumbers();
		}
		
		//methods
		private function kiesRandomNumbers():void{
			for(var i:uint = 1;i<=2;i++){
				number = appModel.random(0, appModel.kaartenArr.length - 1);
				//trace("random number: "+number);
				randomNumberArr.push(number);
			}
			vulUserKaartenArr();
			verwijderItemsUitArray();
		}
		
		private function vulUserKaartenArr():void{
			for each(var number:Number in randomNumberArr){
				userKaartenArr.push(appModel.kaartenArr[number]);	
			}
			trace("userKaarten: "+userKaartenArr);
			display();
		}
		
		private function display():void{
			trace("display [user]");
			//kaarten
			for each (var kaartName:String in userKaartenArr){
				card = new UserCard(kaartName);
				card.dispatchEvent( new Event( MouseEvent.CLICK ) );
				card.x = xPos;
				addChild(card);
				xPos += card.width + 3;
			}
			
			//textField
			userNameDisplay = new TxtField();
			userNameDisplay.txtField_txt.text = username;
			userNameDisplay.y = card.height + 3;
			addChild(userNameDisplay);
		}
		
		//kaarten uit boek verwijderen
		private function verwijderItemsUitArray():void{
			for each(var number:Number in randomNumberArr){
				appModel.kaartenArr.splice(number,1);
			}
			trace("aantal kaarten: "+appModel.kaartenArr.length);
		}
	}
}